package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.service.CheckGroupService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    //创建dao层代理对象
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 分页查询检查组
     * @param queryPageBean
     * @return
     */
    @Override
    public Result findCheckGroupByPage(QueryPageBean queryPageBean) {
        //创建map集合
        HashMap<String, Object> map = new HashMap<>();
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //List<CheckGroup> list = checkGroupMapper.findCheckGroupByPage();
        List<CheckGroup> list = checkGroupMapper.findCheckGroupByQueryString(queryPageBean.getQueryString());
        //判断获取的数据集合是否为空
        if (list == null) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }else{
            //创建pageInfo对象
            PageInfo<CheckGroup> pageInfo = new PageInfo<>(list);
            //将获取的数据存放到集合中
            map.put("list",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,map);
        }
    }

    /**
     * 根据id删除检查组,逻辑删除
     * @param strId
     * @return
     */
    @Override
    public Result deleteCheckGroupById(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
        System.out.println("要删除的id是:"+id);
        //调用方法
        int row = checkGroupMapper.deleteCheckGroupById(id);
        if (row == 0) {
            System.out.println("删除失败");
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }else{
            System.out.println("删除成功");
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }
    }

    /**
     * 添加检查组合相关联的检查项
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @Override
    public Result addCheckGroup(int[] checkitemIds, CheckGroup checkGroup) {
        //回显主键
        //添加检查组
        int row = checkGroupMapper.addCheckGroup(checkGroup);
        //判断检查组是否添加成功
        if (row == 0 || checkitemIds == null || checkitemIds.length == 0){
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }else{
            //添加成功,接着添加检查组与检查项的关联
            boolean flag = addCheckGroupAndCheckItem(checkitemIds,checkGroup.getId());
            //判断
            if (!flag){
                return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
            }else{
                return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
            }
        }
    }

    /**
     * 根据id查询检查组
     * @param strId
     * @return
     */
    @Override
    public Result findCheckGroupById(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
        //创建map集合存储检查组的信息和相关的检查项的id
        CheckGroup checkGroup = checkGroupMapper.findCheckGroupById(id);
        //System.out.println("查询检查组的信息为:"+checkGroup);
        if (checkGroup == null) {
            System.out.println("查询失败");
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }else{
            System.out.println("查询成功");
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        }
    }

    /**
     * 根据检查组的id查询相对应的检查项
     * @param strId
     * @return
     */
    @Override
    public Result findCheckItemIdsByGroupId(String strId) {
        int id = 0;
        if (strId != null) {
            id  = Integer.parseInt(strId);
        }
        System.out.println("检查组的id是:"+id);
        List<Integer> checkItems = checkGroupMapper.findCheckItemIdsByGroupId(id);
        //System.out.println("查询检查组的检查项有:"+checkItems);
        if (checkItems == null) {//|| checkItems.size() == 0
            System.out.println("查询检查项失败");
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }else{
            System.out.println("查询检查项成功");
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkItems);
        }
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @Override
    public Result updateCheckGroupById(int[] checkitemIds, CheckGroup checkGroup) {
        //System.out.println("需要修改的检查组的id是"+checkGroup.getId());
        //删除检查组与检查项的关联
        checkGroupMapper.deleteCheckItemsByCheckGroupId(checkGroup.getId());

        //修改检查组信息
        int row = checkGroupMapper.updateCheckGroupById(checkGroup);
        if (row == 0 || checkitemIds == null || checkitemIds.length == 0){
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }else{
            //将先关联的检查项id添加到关联表中
            boolean flag = addCheckGroupAndCheckItem(checkitemIds,checkGroup.getId());
            //判断
            if (!flag){
                return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
            }else{
                return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
            }
        }
    }

    //新增检查组与检查项的关联
    public boolean addCheckGroupAndCheckItem(int[] checkitemIds,int id){
        int num = 0;
        for (int i = 0; i < checkitemIds.length; i++) {
            checkGroupMapper.addCheckGroupAndCheckItem(id,checkitemIds[i]);
            num++;
        }
        //判断
        if (num != checkitemIds.length ){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Result findAllCheckGroup() {
        //创建map集合存储检查组的信息和相关的检查项的id
        List<CheckGroup> list = checkGroupMapper.findAllCheckGroup();
        //System.out.println("查询检查组的信息为:"+checkGroup);
        if (list == null || list.size() == 0) {
            System.out.println("查询失败");
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }else{
            System.out.println("查询成功");
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
        }
    }
}
